#---------------------------------------#
#---------------------------------------#
# Data Preparation Script
# Article: Partisan responses to democracy promotion – Estimating the causal effect of a civic information portal
# Journal: World Development
# Date: 2020-02-10
#---------------------------------------#
#---------------------------------------#

#---------------------------------------#
#Data
#---------------------------------------#

path <- c("[path]")
setwd(path)
load("Mzalendo_RCT_data_raw.RData")


#---------------------------------------#
# Coding
#---------------------------------------#

#-------------------------#
# Subject Pool - Full data

# Partisanship
# Full Non-Constituency Coding (only 2 Qs)
raw$pro.govt.raw <- raw$s1_q14b
levels(raw$pro.govt.raw)[levels(raw$pro.govt.raw)==""] <- "Non-Partisan"
levels(raw$pro.govt.raw)[levels(raw$pro.govt.raw)=="Neither"] <- "Undeclared"
raw$pro.govt.raw <- relevel(raw$pro.govt.raw, ref="Opposition")
# Dummy version (the literal PAP version)
raw$progovt.other <- ifelse(raw$pro.govt.raw=="Government",T,F)

# Constituency-Level Partisanship (all 3 Qs)
raw$s2_q15.d <- ifelse(raw$s2_q15=="Yes",T,F)
raw$opp.in.rp.const <- ifelse(raw$s2_q14b=="Opposition" & raw$s2_q15=="Yes",T,F)

raw$pro.govt.full <- NA
raw$pro.govt.full[raw$pro.govt.raw=="Opposition" & raw$s2_q15.d==T] <- "Oppos-in-Govt"
raw$pro.govt.full[raw$pro.govt.raw=="Opposition" & raw$s2_q15.d==F] <- "Oppos-in-Non-Govt"
raw$pro.govt.full[raw$pro.govt.raw=="Non-Partisan" & raw$s2_q15.d==T] <- "Non-Part-in-Govt"
raw$pro.govt.full[raw$pro.govt.raw=="Non-Partisan" & raw$s2_q15.d==F] <- "Non-Part-in-NGovt"
raw$pro.govt.full[raw$pro.govt.raw=="Government" & raw$s2_q15.d==T] <- "Govt-in-Govt"
raw$pro.govt.full[raw$pro.govt.raw=="Government" & raw$s2_q15.d==F] <- "Govt-in-Non-Govt"
raw$pro.govt.full[raw$pro.govt.raw=="Undeclared" & raw$s2_q15.d==T] <- "Undecl-in-Govt"
raw$pro.govt.full[raw$pro.govt.raw=="Undeclared" & raw$s2_q15.d==F] <- "Undecl-in-Non-Govt"
# Reference category
raw$pro.govt.full <- relevel(as.factor(raw$pro.govt.full), ref="Oppos-in-Govt")

# or, simply
raw$pro.govt.in.govt.na <- ifelse(raw$s2_q15.d==T & raw$pro.govt.raw=="Government", T, F)

# Three-way Partisanship
raw$pro.govt.3way <- raw$pro.govt.full
levels(raw$pro.govt.3way)[levels(raw$pro.govt.3way)=="Oppos-in-Govt"] <- "Loser"
levels(raw$pro.govt.3way)[levels(raw$pro.govt.3way)=="Govt-in-Non-Govt"] <- "Partial Winner"
levels(raw$pro.govt.3way)[levels(raw$pro.govt.3way)=="Oppos-in-Non-Govt"] <- "Partial Winner"
levels(raw$pro.govt.3way)[levels(raw$pro.govt.3way)=="Govt-in-Govt"] <- "Winner"
raw$pro.govt.3way <- relevel(raw$pro.govt.3way, ref="Loser")


#-------------------------#
# Subject Pool - Full data
data <- raw[!is.na(raw$s2_q15) & raw$s2_q15!="",]

# Create Ddmmies
data$s2_q7.d <- ifelse(data$s2_q7>=3,T,F)
data$s1_q7.d <- ifelse(data$s1_q7>=3,T,F)
data$q7.d.diff <- data$s2_q7.d-data$s1_q7.d
data$q7.diff <- data$s2_q7-data$s1_q7
data$s2_q6.d <- ifelse(data$s2_q6>=3,T,F)
data$s1_q6.d <- ifelse(data$s1_q6>=3,T,F)
data$q6.d.diff <- data$s2_q6.d-data$s1_q6.d
data$q6.diff <- data$s2_q6-data$s1_q6


#---------------------------------------#
# Save it
save(data, raw, subset, file = "Mzalendo_RCT_data.RData")

